# -*- coding: utf-8 -*-
# Yuanjiang Huang
# yuanjiang.huang@socialcredits.cn

#百度招聘的数据预处理


from scpy.logger import get_logger
import os
import sys
import re
mode = re.compile(r'\d+')
logger = get_logger(__file__)

CURRENT_PATH = os.path.dirname(__file__)
if CURRENT_PATH:
    CURRENT_PATH = CURRENT_PATH + "/"
#定义需要提取的字段，保持与名字跟baidu返回的一致
needed_info_from_job = ['scale','salary','degree','entity','requireNum','workingYears']

def between(value,a = None,b = None):
    '''
    :param value: 输入的值
    :param a: 下限
    :param b: 上限
    :return: 是否在指定的范围
    '''

    if (a != None) and (b != None):
        if (value >= a) and ( value <= b):
            return True
        else:
            return False
    elif (a == None) and (b != None):
        if (value <= b):
            return True
        else:
            return  False
    elif (a != None) and (b == None):
        if (value >=a ):
            return True
        else:
            return False
    else:
        return False

#将公司规模scale换成一个平均值
def trans_scale(scale_before):
    '''
    :param scale_before: 公司规模的描述, 形如100-200人，100人以上
    :return: 返回公司规模，求平均，或者返回出现过的数字，否则返回None
    '''
    res = {'before': scale_before, 'after': None}
    try:
        values = mode.findall(scale_before.encode('utf-8'))
    except Exception,e:
        # logger.info('trans_scale mode.findall error; res-after set to be None ！')
        # logger.info('*'*100)
        res['after'] = None
        return res
    try:
        if len(values) != 0:
            # 求平均
            if len(values) >= 2:
                average = (float(values[0]) + float(values[-1]))/2
            else:
            #如果单值就返回取到的单值
                average = float(values[0])
            res['after'] = average
        else:
            res['after'] = None
    except Exception,e:
        # logger.info('trans scale error！')
        # logger.info(scale_before)
        res['after'] = None
    return res


def trans_salary(salary_before):
    res = {'before': salary_before, 'after': None}
    try:
        values = mode.findall(salary_before.encode('utf-8'))
    except Exception,e:
        logger.info('trans_salary mode.findall error; res-after set to be None ！')
        logger.info('*'*100)
        logger.info(salary_before)
        logger.info(type(salary_before))
        res['after'] = None
        return res
    try:
        if salary_before.find(u"元/天") != -1:
            res['after'] = float(values[0]) * 30
            return res
        if salary_before.find(u"元/次") != -1:
            res['after'] = None
            return res
        if salary_before.find(u"元/小时") != -1:
            res['after'] = float(values[0]) * 8 * 30
            return res
        if (salary_before.find(u"面谈") != -1) or (salary_before.find(u"面议") != -1):
            res['after'] = None
            return res
        if len(values) != 0:
            if len(values) >= 2:
                if salary_before.find(u"k") != -1 or salary_before.find(u"K") != -1:
                    average = (float(values[0]) * 1000 + float(values[-1]) * 1000)/2
                elif salary_before.find(u"年") != -1 and salary_before.find(u"k") != -1:
                    average = (float(values[0]) * 1000 + float(values[-1]) * 1000)/2/12
                elif salary_before.find(u"年") != -1:
                    average = (float(values[0]) + float(values[-1]))/2/12
                else:
                    average = (float(values[0]) + float(values[-1]))/2

            else:
                if salary_before.find(u"k") != -1 or salary_before.find(u"K") != -1:
                    average = float(values[0]) * 1000
                elif salary_before.find(u"年") != -1:
                    average = float(values[0])/12
                else:
                    average = float(values[0])
            res['after'] = average
        else:
            res['after'] = None
    except Exception, e:
        logger.info('trans salary error！')
        print salary_before
        res['after'] = None
    ##abnormal monthly salary
    #if (res['after']) < 800 or (res['after'] >= 200000):
    #    res['after'] = None
    return res



def trans_degree(degree_before):
    '''
    :param degree_before: 对求职者学历的描述
    :return: 学历转换为指定标签，如果转换失败，返回None
    '''
    # 学历转换为统一格式
    res = {'before': degree_before, 'after': None}
    try:
        if degree_before == '':
            logger.warn('The degree info. is missing, set is to 学历不限')
            res['after'] = u"学历不限"
            return res
        if degree_before.find(u"本科") != -1:
            res['after'] = u"本科"
            return res
        if degree_before.find(u"硕士") != -1:
            res['after'] = u"硕士"
            return res
        if degree_before.find(u"博士") != -1:
            res['after'] = u"博士"
            return res
        if degree_before.find(u"不限") != -1 or degree_before.find(u"应届毕业生") != -1:
            res['after'] = u"学历不限"
        else:
            res['after'] = u"本科以下"
    except Exception,e:
        # logger.info('trans degree error！')
        # print degree_before
        res['after'] = None
    return res


def trans_entity(entity_before):
    '''
    :param entity_before: 对公司类别的描述
    :return: 公司类别转换为指定类型，如果没有找到，返回 私营/民营
    '''
    res = {'before': entity_before, 'after': None}
    try:
        if (entity_before.find(u"民营") != -1) or (entity_before.find(u"私营") != -1):
            res['after'] = u"私营/民营"
            return res
        if entity_before.find(u'事业') != -1:
            res['after'] = u"事业单位"
            return res
        if (entity_before.find(u'外商') != -1) or (entity_before.find(u'合资') != -1)\
            or (entity_before.find(u'外企') != -1):
            res['after'] = u"外商投资企业"
            return res
        if entity_before.find(u'国企') != -1:
            res['after'] = u"国企"
            return res
        if (entity_before.find(u'政府') != -1) or (entity_before.find(u'国家') != -1):
            res['after'] = u"政府机关"
            return res
        if entity_before.find(u'股份') != -1:
            res['after'] = u"股份制"
            return res
        if entity_before.find(u'上市') != -1:
            res['after'] = u"上市公司"
        else:
            res['after'] = u"私营/民营"
    except Exception, e:
        # logger.info('trans entity error！Check the entity name format')
        # logger.info(entity_before)
        res['after'] = None
    return res


def trans_req_num(req_num_before):
    '''
    :param req_num_before: 职位数量要求的描述
    :return:返回此岗位求职者数量，默认为1
    '''
    res = {'before': req_num_before, 'after': None}
    try:
        if isinstance(req_num_before,int):
            if req_num_before == 0:
                logger.warn('The original required num is 0, now it is set to be 1')
                res['after'] = 1
            return res
        else:
            values = mode.findall(req_num_before.encode('utf-8'))
    except Exception,e:
        # logger.info('req_num_before mode.findall error; res-after set to be None ！')
        # logger.info('*'*100)
        # logger.info(req_num_before)
        res['after'] = 1
        return res
    try:
        if len(values) != 0:
            if len(values) >= 2:
                average = int((float(values[0]) + float(values[-1]))/2)
            else:
                average = int(float(values[0]))
            res['after'] = average
        else:
            res['after'] = 1
    except Exception,e:
        # logger.info('trans req_num error！')
        # print req_num_before
        res['after'] = 1
    return res